//@author A0097091Y
//This class stores the details for each task

package crossoff.model;

import org.joda.time.DateTime;

public class Task implements Comparable<Task> {

	private int taskID;
	private String description;
	private DateTime start;
	private DateTime end;
	private DateTime dateCreated;

	public Task() {
		description = "";
		start = null;
		end = null;
		dateCreated = new DateTime();
		taskID = this.hashCode();

	}

	public Task(String newDescription) {
		description = newDescription;
		start = null;
		end = null;
		dateCreated = new DateTime();
		taskID = this.hashCode();

	}

	public Task(String newDescription, DateTime newStart) {
		description = newDescription;
		start = newStart;
		end = null;
		dateCreated = new DateTime();
		taskID = this.hashCode();
	}

	public Task(String newDescription, DateTime newStart, DateTime newEnd) {
		description = newDescription;
		start = newStart;
		end = newEnd;
		dateCreated = new DateTime();
		taskID = this.hashCode();
	}

	public Task makeCopy() {
		Task temp = new Task(description, start, end);
		temp.setTaskID(taskID);
		return temp;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public DateTime getStart() {
		return start;
	}

	public void setStart(DateTime start) {
		this.start = start;
	}

	public DateTime getEnd() {
		return end;
	}

	public void setEnd(DateTime end) {
		this.end = end;
	}

	public int getTaskID() {
		return taskID;
	}

	public void setTaskID(int taskID) {
		this.taskID = taskID;
	}

	public DateTime getDateCreated() {
		return dateCreated;
	}

	public void setDateCreated(DateTime dateCreated) {
		this.dateCreated = dateCreated;
	}
	
	//@author A0091838U
	@Override
	public int compareTo(Task otherTask) {
		if (end != null) {
			if (otherTask.getEnd() != null) {
				return end.compareTo(otherTask.getEnd());
			} else if (otherTask.getStart() != null) {
				return end.compareTo(otherTask.getStart());
			} else {
				return -1;
			}
		}

		else if (start != null) {
			if (otherTask.getStart() != null) {
				return start.compareTo(otherTask.getStart());
			} else if (otherTask.getEnd() != null) {
				return start.compareTo(otherTask.getEnd());
			} else {
				return -1;
			}
		}

		else if (start == null && end == null) {
			if (otherTask.getEnd() != null || otherTask.getStart() != null) {
				return 1;
			}
		}

		return description.compareToIgnoreCase(otherTask.getDescription());
	}
	
	//@author generated
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((dateCreated == null) ? 0 : dateCreated.hashCode());
		result = prime * result
				+ ((description == null) ? 0 : description.hashCode());
		result = prime * result + ((end == null) ? 0 : end.hashCode());
		result = prime * result + ((start == null) ? 0 : start.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Task other = (Task) obj;
		if (dateCreated == null) {
			if (other.dateCreated != null)
				return false;
		} else if (!dateCreated.equals(other.dateCreated))
			return false;
		if (description == null) {
			if (other.description != null)
				return false;
		} else if (!description.equals(other.description))
			return false;
		if (end == null) {
			if (other.end != null)
				return false;
		} else if (!end.equals(other.end))
			return false;
		if (start == null) {
			if (other.start != null)
				return false;
		} else if (!start.equals(other.start))
			return false;
		return true;
	}

}
